﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ETADTools.Lib
{
  public static partial class Converter
  {
    /// <summary>
    /// 
    /// </summary>
    /// <param name="tableName"></param>
    /// <returns></returns>
    public static string TableNameToTableAlias(string tableName)
    {
      if (tableName == string.Empty)
      {
        throw new ArgumentNullException("Table name");
      }

      // Remove first letter t in table name if existed
      if (tableName[0] == 't')
      {
        tableName = tableName.Substring(1, tableName.Length - 1);
      }

      return tableName;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="tableName"></param>
    /// <returns></returns>
    public static string TableNameToPrimaryKey(string tableName)
    {
      if (tableName == string.Empty)
      {
        throw new ArgumentNullException("Table name");
      }

      // Remove first letter t in table name if existed
      if (tableName[0] == 't')
      {
        tableName = tableName.Substring(1, tableName.Length - 1);
      }

      // Add ID to table name
      tableName = tableName + "ID";

      return tableName;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="tableName"></param>
    /// <returns></returns>
    public static string TableNameToWords(string tableName)
    {
      StringBuilder words = new StringBuilder();
      string word = string.Empty;

      for (int i = 0; i < tableName.Length; i++)
      {
        if (char.IsUpper(tableName[i]))
        {
          // Get the first uppercase character
          word += tableName[i];

          // Scan forward
          for (int j = i + 1; j < tableName.Length; j++)
          {
            if (char.IsLower(tableName[j]))
            {
              word += tableName[j];
            }
            else
            {
              break;
            }
          }

          if (word == "Emp")
          {
            word = "Employee";
          }

          words.Append(word);
          words.Append(" ");

          word = string.Empty;
        }
      }

      return words.ToString();
    }

  }
}
